package cn.play.cardgame.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.play.cardgame.R;
import cn.play.cardgame.util.ImageUtil;
import cn.play.cardgame.util.LogUtil;

public class InfoPanel extends LinearLayout {
    @InjectView(R.id.info_balance)
    ImageView mTvBalance;
    @InjectView(R.id.info_bet)
    ImageView mTvBet;
    @InjectView(R.id.info_win)
    ImageView mTvWin;
    @InjectView(R.id.info_last_win)
    ImageView mTvLastWin;
    private Context mContext;
    private float balance;
    private float bet;
    private float win;
    private float lastWin;

    public InfoPanel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initViews();
    }

    public InfoPanel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InfoPanel(Context context) {
        this(context, null);
    }

    private void initViews() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        View view = View.inflate(mContext, R.layout.info_panel, null);
        ButterKnife.inject(this, view);
        params.gravity = Gravity.CENTER;
        this.addView(view, params);

        setBalance(0);
        setBet(0);
        setWin(0);
        setLastWin(0);
    }

    public float getBalance() {
        return balance;
    }

    public float getBet() {
        return bet;
    }

    public float getWin() {
        return win;
    }

    public float getLastWin() {
        return lastWin;
    }

    private String trimDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }

    @SuppressWarnings("deprecation")
    public void setBalance(float balance) {
        LogUtil.getLogger().d("info   info:" + balance);
        this.balance = balance;
        String mBalance = "0";
        try {
            mBalance = (String.valueOf(balance)).split("\\.")[0];
        } catch (Exception e) {
            e.printStackTrace();
            mBalance = String.valueOf(balance);
        }
        mTvBalance.setBackgroundDrawable(ImageUtil.getInstance()
                .bitmapToDrawable(mContext, ImageUtil.getInfoMapping(),
                        trimDot(mBalance)));
    }

    @SuppressWarnings("deprecation")
    public void setBet(float bet) {
        this.bet = bet;
        mTvBet.setBackgroundDrawable(ImageUtil.getInstance().bitmapToDrawable(
                mContext, ImageUtil.getInfoMapping(), trimDot(String.valueOf(bet))));
    }

    @SuppressWarnings("deprecation")
    public void setWin(float win) {
        this.win = win;
        String mWin = "0";
        try {
            mWin = (String.valueOf(win)).split("\\.")[0];
        } catch (Exception e) {
            e.printStackTrace();
            mWin = String.valueOf(win);
        }
        mTvWin.setBackgroundDrawable(ImageUtil.getInstance().bitmapToDrawable(
                mContext, ImageUtil.getInfoMapping(), trimDot(mWin)));
    }

    @SuppressWarnings("deprecation")
    public void setLastWin(float lastWin) {
        this.lastWin = lastWin;
        String mLastWin;
        try {
            mLastWin = (String.valueOf(lastWin)).split("\\.")[0];
        } catch (Exception e) {
            e.printStackTrace();
            mLastWin = String.valueOf(lastWin);
        }
        mTvLastWin.setBackgroundDrawable(ImageUtil.getInstance()
                .bitmapToDrawable(mContext, ImageUtil.getInfoMapping(),
                        trimDot(mLastWin)));
    }

}
